[20220812_134920_user] D2_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};
  40. uint16_t i,val,value_H,value_L,Cvalue,Hvalue;
  41. int error;
  42. float C1=-2.0468;
  43. float C2=0.0367;
  44. float C3=-0.0000015955;
  45. float RH_Lin;
  46. float RH_Ture;
  47. float d1=-39.6;
  48. float d2=0.01;
  49. float T1=0.01;
  50. float T2=0.00008;
  51. float temp_C=0;
  52. int temp;
  53. int RH;
  54. float M14_SHT11 =0;
  55. /* USER CODE BEGIN PV */
  56. typedef void (*pFunction)(void);
  57. /* USER CODE END PV */
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. void MX_ADC1_Init1(char pin);
  61. void sensor(void);
  62. void SHT10_TransStart(void);
  63. void SHT10_WriteByte(void);
  64. void SHT10_WriteByte2(void);
  65. void SHT10_ReadByte(void);
  66. void SHT10_Calculate(void);
  67. void MX_GPIO_Input1(unsigned long pin);
  68. /* USER CODE BEGIN PFP */
  69. /* USER CODE END PFP */
  70. /* Private user code ---------------------------------------------------------*/
  71. /* USER CODE BEGIN 0 */
  72. int _write(int fd, char *ch, int len)
  73. {
  74. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  75. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  76. return len;
  77. }
  78. void print(char* s, float c)
  79. { int w;
  80. int w_d;
  81. int w_f;
  82. if(c<0)
  83. {
  84. w = c*100;
  85. w_d = w/100;
  86. w_f = (-1*w)%100;
  87. }
  88. else
  89. {
  90. w = c*100;
  91. w_d = w/100;
  92. w_f = w%100;
  93. }
  94. printf("%s = %d.%d\n",s,w_d,w_f);
  95. }
  96. /* USER CODE END 0 */
  97. /**
  98. * @brief The application entry point.
  99. * @retval int
  100. */
  101. int main(void)
  102. {
  103. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  104. HAL_Init();
  105. SystemClock_Config();
  106. MX_GPIO_Init();
  107. MX_USART2_UART_Init();
  108. MX_UART4_Init();
  109. MX_USART1_UART_Init();
  110. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  111. HAL_Delay(1000);
  112. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  113. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  114. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  115. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  116. {
  117. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  118. }
  119. else if(zigbeestatu[18]== 0x05)
  120. {
  121. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  122. }
  123. while (1)
  124. {
  125. sensor();
  126. if(M14_SHT11<16)
  127. {
  128. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
  129. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  130. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  131. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  132. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  133. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  134. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  135. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
  136. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  137. }
  138. }
  139. }
  140. void SystemClock_Config(void)
  141. {
  142. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  143. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  144. __HAL_RCC_PWR_CLK_ENABLE();
  145. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  146. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  147. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  148. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  149. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  150. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  151. RCC_OscInitStruct.PLL.PLLM = 8;
  152. RCC_OscInitStruct.PLL.PLLN = 72;
  153. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  154. RCC_OscInitStruct.PLL.PLLQ = 3;
  155. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  156. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  157. {
  158. Error_Handler();
  159. }
  160. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  161. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  162. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  163. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  164. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  165. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  166. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. }
  171. void user_pwm_setvalue(uint16_t value)
  172. {
  173. TIM_OC_InitTypeDef sConfigOC;
  174. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  175. sConfigOC.Pulse = value;
  176. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  177. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  178. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  179. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  180. }
  181. void user_pwm_setvalue2(uint16_t value)
  182. {
  183. TIM_OC_InitTypeDef sConfigOC;
  184. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  185. sConfigOC.Pulse = value;
  186. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  187. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  188. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  189. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  190. }
  191. void sensor(void)
  192. {
  193. Cvalue=0;
  194. Hvalue=0;
  195. value_H=0;
  196. value_L=0;
  197. SHT10_TransStart();
  198. SHT10_WriteByte();
  199. MX_GPIO_Input1(GPIO_PIN_0);
  200. HAL_Delay(250);
  201. if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
  202. {
  203. SHT10_ReadByte();
  204. value_H=val;
  205. SHT10_ReadByte();
  206. value_L=val;
  207. Cvalue = (value_H<< 8 | value_L);
  208. }
  209. SHT10_TransStart();
  210. SHT10_WriteByte2();
  211. MX_GPIO_Input1(GPIO_PIN_0);
  212. HAL_Delay(250);
  213. if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
  214. {
  215. SHT10_ReadByte();
  216. value_H=val;
  217. SHT10_ReadByte();
  218. value_L=val;
  219. Hvalue = (value_H<< 8 | value_L);
  220. }
  221. SHT10_Calculate();
  222. HAL_Delay(10000);
  223. }
  224. void SHT10_TransStart(void)
  225. {
  226. MX_GPIO_Init1();
  227. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  228. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  229. HAL_Delay(10);
  230. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  231. HAL_Delay(10);
  232. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  233. HAL_Delay(10);
  234. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  235. HAL_Delay(10);
  236. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  237. HAL_Delay(10);
  238. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  239. HAL_Delay(10);
  240. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  241. HAL_Delay(10);
  242. }
  243. void SHT10_WriteByte(void)
  244. {
  245. MX_GPIO_Init1();
  246. for (i=0x80;i>0;i/=2)
  247. {
  248. if (i & 0x03)
  249. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  250. else
  251. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  252. HAL_Delay(10);
  253. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  254. HAL_Delay(10);
  255. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  256. HAL_Delay(10);
  257. }
  258. MX_GPIO_Input1(GPIO_PIN_0);
  259. HAL_Delay(10);
  260. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  261. HAL_Delay(10);
  262. error=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
  263. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  264. HAL_Delay(10);
  265. }
  266. void SHT10_WriteByte2(void)
  267. {
  268. MX_GPIO_Init1();
  269. for (i=0x80;i>0;i/=2)
  270. {
  271. if (i & 0x05)
  272. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  273. else
  274. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  275. HAL_Delay(10);
  276. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  277. HAL_Delay(10);
  278. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  279. HAL_Delay(10);
  280. }
  281. MX_GPIO_Input1(GPIO_PIN_0);
  282. HAL_Delay(10);
  283. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  284. HAL_Delay(10);
  285. error=HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
  286. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  287. HAL_Delay(10);
  288. }
  289. void SHT10_ReadByte(void)
  290. {
  291. val=0;
  292. MX_GPIO_Input1(GPIO_PIN_0);
  293. for (i=0x80;i>0;i/=2)
  294. {
  295. HAL_Delay(10);
  296. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  297. HAL_Delay(10);
  298. if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
  299. val=( val | i );
  300. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  301. }
  302. MX_GPIO_Init1();
  303. if (1)
  304. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  305. else
  306. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  307. HAL_Delay(10);
  308. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  309. HAL_Delay(10);
  310. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  311. HAL_Delay(10);
  312. }
  313. void SHT10_Calculate(void)
  314. {
  315. temp_C=d1+d2*Cvalue;
  316. RH_Lin = C1+C2*Hvalue+C3*Hvalue*Hvalue;
  317. RH_Ture= (temp_C-25)*(T1+T2*Hvalue)+RH_Lin;
  318. if (RH_Ture>100)
  319. RH_Ture= 100;
  320. if (RH_Ture<0.1)
  321. RH_Ture=0.1;
  322. if (temp_C<0)
  323. {
  324. cmd[1]=1;
  325. cmd[2]=-1*temp_C;
  326. temp=(-10*temp_C);
  327. cmd[3]=temp%10;
  328. cmd[4]=RH_Ture;
  329. RH=RH_Ture*10;
  330. cmd[5]=RH%10;
  331. }
  332. else
  333. {
  334. cmd[1]=0;
  335. cmd[2]=temp_C;
  336. temp=(10*temp_C);
  337. cmd[3]=temp%10;
  338. cmd[4]=RH_Ture;
  339. RH=RH_Ture*10;
  340. cmd[5]=RH%10;
  341. }
  342. }
  343. void MX_ADC1_Init1(char pin)
  344. {
  345. ADC_ChannelConfTypeDef sConfig = {0};
  346. hadc1.Instance = ADC1;
  347. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  348. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  349. hadc1.Init.ScanConvMode = DISABLE;
  350. hadc1.Init.ContinuousConvMode = DISABLE;
  351. hadc1.Init.DiscontinuousConvMode = DISABLE;
  352. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  353. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  354. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  355. hadc1.Init.NbrOfConversion = 1;
  356. hadc1.Init.DMAContinuousRequests = DISABLE;
  357. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  358. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  359. {
  360. Error_Handler();
  361. }
  362. sConfig.Channel = pin;
  363. sConfig.Rank = 1;
  364. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  365. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  366. {
  367. Error_Handler();
  368. }
  369. }
  370. void MX_GPIO_Input1(unsigned long pin)
  371. {
  372. GPIO_InitTypeDef GPIO_InitStruct = {0};
  373. __HAL_RCC_GPIOE_CLK_ENABLE();
  374. GPIO_InitStruct.Pin = pin;
  375. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  376. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  377. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  378. }
  379. void Error_Handler(void)
  380. {
  381. }